<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2022/6/21
 * Time: 10:08
 */

namespace app\project\controller;


use app\common\controller\Common;
use app\project\service\PublicService;
use think\Controller;
use think\Exception;
use think\Session;
use think\Validate;

class Policy extends Controller
{

    //惠企政策字典配置主页面
    public function index(){
        //权限查询
        $common = new Common();

        if(!empty($_GET['list_type'])){
            $where['type'] =  $_GET['list_type'];
        }else{
            $where['type'] =  ['in',[9,10,11]];
        }

        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['pageSize']) ? 10 : $_GET['pageSize'];
                $start = ($page - 1) * $limit;
                $thisWechat = Session::get("thisWechat");
                $where['uniacid'] = $thisWechat['id'];
                $where['status'] = ['gt',0];
                $list = $common->public_select("project_type",$where,null,$start,$limit)->order("id desc")->select();
                $count = $common->public_select("project_type",$where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return $this->fetch("index");
    }

    //详情视图
    public function detail(){
        $thisWechat = Session::get("thisWechat");
        $id = empty($_GET['id'])?0:$_GET['id'];
        $where['id'] = $id;
        $where['uniacid'] = $thisWechat['id'];
        $detail = PublicService::detailService("project_type",$where);
        $this->assign('detail',$detail);
        return view();
    }

    //保存操作
    public function save(){
        $thisWechat = Session::get("thisWechat");
        try{
            $common = new Common();
            $rule = [
                ["name", "require", "名称不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $data['addtime'] = date("Y-m-d H:i:s",time());
            $data['add_id'] = Session::get("id");
            $id = $common->handle("project_type",$data,1);
            $log['remark'] = "账号【" . Session::get("user_name") . "】添加项目类别【".$data['name']."】id【".$id."】";
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //删除操作
    public function del(){
        try{
            $common = new Common();
            $id = $_POST['id'];
            $table = $_POST['table'];
            $power = $common->public_select($table,['id'=>['in',$id]])->select();
            $res = $common->public_select($table,['id'=>['in',$id]])->delete();

            $common->public_select($table,['pid'=>['in',$id]])->delete();
            if(empty($res)){
                throw new Exception("删除失败");
            }

            foreach ($power as $k=>$v){
                $log['remark'] = "账号【" . Session::get("user_name") . "】删除项目类别【".$v['name']."】id【".$v['id']."】";
                $common->addLog($log);
            }
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}